﻿using System;
using System.Text;
using System.Collections.Generic;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using UT_AMS.DataAccess.BO;
using AMS.DataAccess.Sql;
using System.Data;
using AMS.DataAccess;

namespace UT_AMS.DataAccess
{
    /// <summary>
    /// Summary description for UT_SqlInsert
    /// </summary>
    [TestClass]
    public class UT_BusinessObjectMapping
    {
        public UT_BusinessObjectMapping()
        {
            //
            // TODO: Add constructor logic here
            //
        }

        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        //
        // You can use the following additional attributes as you write your tests:
        //
        // Use ClassInitialize to run code before running the first test in the class
        // [ClassInitialize()]
        // public static void MyClassInitialize(TestContext testContext) { }
        //
        // Use ClassCleanup to run code after all tests in a class have run
        // [ClassCleanup()]
        // public static void MyClassCleanup() { }
        //
        // Use TestInitialize to run code before running each test 
        // [TestInitialize()]
        // public void MyTestInitialize() { }
        //
        // Use TestCleanup to run code after each test has run
        // [TestCleanup()]
        // public void MyTestCleanup() { }
        //
        #endregion

        private DataTable MakeTableSchema(bool addNoMappingColumn)
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("AID", typeof(int));
            dt.Columns.Add("Name", typeof(string));
            dt.Columns.Add("Age", typeof(int));
            dt.Columns.Add("BooksCount", typeof(int));
            dt.Columns.Add("Birth", typeof(DateTime));
            dt.Columns.Add("GuidField", typeof(Guid));
            dt.Columns.Add("GuidNullableField", typeof(Guid));
            dt.Columns.Add("CommandUT", typeof(int));
            //dt.Columns.Add("UserType2", typeof(int));
            if (addNoMappingColumn)
            {
                dt.Columns.Add("NoDbClumn", typeof(string));
            }

            return dt;
        }

        private UserInfo CreateUser()
        {
            UserInfo user = new UserInfo();
            user.AID = 1;
            user.Age = 100;
            user.Birthday = DateTime.Now;
            user.BooksCount = 1345;
            user.CommandUT = UserType.Company;
            user.GuidField = Guid.NewGuid();
            user.Name = "小明";
            user.NoDbClumn = "no value";
            user.UserType2 = UserType.Orgnization;
            return user;
        }

        [TestMethod]
        public void TestBasic_BOToDataRow()
        {
            UserInfo user = CreateUser();

            DataTable dt = MakeTableSchema(true);
            DataRow row = dt.NewRow();
            BusinessObjectMapping.BOToDataRow<UserInfo>(user, row);
            dt.Rows.Add(row);

            Assert.AreEqual<int>(user.AID, (int)row["AID"]);
            Assert.AreEqual<string>(user.Name, (string)row["Name"]);
            Assert.AreEqual<DateTime>(user.Birthday, (DateTime)row["Birth"]);
            Assert.AreEqual<Guid>(user.GuidField, (Guid)row["GuidField"]);
            Assert.IsTrue(row["NoDbClumn"] == DBNull.Value);
        }

        [TestMethod]
        public void Test_Table的枚举类型为字符串()
        {
            UserInfo user = CreateUser();
            user.UserType2 = UserType.Orgnization;

            DataTable dt = MakeTableSchema(true);
            dt.Columns.Add("UserType2", typeof(string));

            DataRow row = dt.NewRow();
            BusinessObjectMapping.BOToDataRow<UserInfo>(user, row);
            dt.Rows.Add(row);

            Assert.AreEqual<int>(user.AID, (int)row["AID"]);
            Assert.IsTrue(row["NoDbClumn"] == DBNull.Value);
            Assert.IsTrue(row["UserType2"] != DBNull.Value);
            Assert.AreEqual<string>("Orgnization", (string)row["UserType2"]);
        }

        [TestMethod]
        public void TestBasic_DataRowToBO()
        {
            UserInfo user = CreateUser();

            DataTable dt = MakeTableSchema(true);
            DataRow row = dt.NewRow();
            BusinessObjectMapping.BOToDataRow<UserInfo>(user, row);
            dt.Rows.Add(row);

            UserInfo user2 = BusinessObjectMapping.DataRowToBO<UserInfo>(row);

            Assert.AreEqual<int>(user.AID, user2.AID);
            Assert.AreEqual<string>(user.Name, user2.Name);
            Assert.AreEqual<DateTime>(user.Birthday, user2.Birthday);
            Assert.AreEqual<Guid>(user.GuidField, user2.GuidField);
            Assert.IsTrue(user2.NoDbClumn == null);
            Assert.IsTrue(user2.NoDbClumn != user.NoDbClumn);
        }

        [TestMethod]
        public void TestDataRowToBO_枚举类型为字符串()
        {
            UserInfo user = CreateUser();
            user.UserType2 = UserType.Orgnization;

            DataTable dt = MakeTableSchema(true);
            dt.Columns.Add("UserType2", typeof(string));

            DataRow row = dt.NewRow();
            BusinessObjectMapping.BOToDataRow<UserInfo>(user, row);
            dt.Rows.Add(row);

            UserInfo user2 = BusinessObjectMapping.DataRowToBO<UserInfo>(row);

            Assert.AreEqual<int>(user.AID, user2.AID);
            Assert.AreEqual<UserType>(UserType.Orgnization, user2.UserType2);
            Assert.AreEqual<UserType>(user.UserType2, user2.UserType2);
        }


        [TestMethod]
        public void TestBasic_DataRowToBO_列名大小写不一致()
        {
            UserInfo user = CreateUser();

            DataTable dt = MakeTableSchema(true);
            for (int i = 0; i < dt.Columns.Count; i++)
            {
                dt.Columns[i].ColumnName = dt.Columns[i].ColumnName.ToUpper();
            }

            DataRow row = dt.NewRow();
            BusinessObjectMapping.BOToDataRow<UserInfo>(user, row);
            dt.Rows.Add(row);

            UserInfo user2 = BusinessObjectMapping.DataRowToBO<UserInfo>(row);

            Assert.AreEqual<int>(user.AID, user2.AID);
            Assert.AreEqual<string>(user.Name, user2.Name);
            Assert.AreEqual<DateTime>(user.Birthday, user2.Birthday);
            Assert.AreEqual<Guid>(user.GuidField, user2.GuidField);
            Assert.IsTrue(user2.NoDbClumn == null);
            Assert.IsTrue(user2.NoDbClumn != user.NoDbClumn);
        }
    }
}
